package com.webcamtracker.ui.actions;

import com.webcamtracker.colortracker.TrackerManager;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;


public class StartStopAction implements Action {


    boolean isStarted = false;
    private PropertyChangeListener listener;
    public static final String START_TRAKING = "Start Tracking";
    public static final String STOP_TRAKING = "Stop Tracking";
    private TrackerManager manager;

    public StartStopAction(TrackerManager manager) {

        this.manager = manager;
    }

    public boolean isEnabled() {
        return true;
    }

    public void setEnabled(boolean b) {
    }

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        this.listener = listener;
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        this.listener = null;
    }

    public Object getValue(String key) {
        String result = null;

        if (key.equals(Action.NAME)) {
            if (!isStarted) {
                result = START_TRAKING;
            } else {
                result = STOP_TRAKING;
            }
        }

        return result;
    }

    public void putValue(String key, Object value) {
    }

    public void actionPerformed(ActionEvent e) {
        if (!isStarted) {
            manager.startTracking();
            isStarted = true;
            listener.propertyChange(new PropertyChangeEvent(e.getSource(), Action.NAME, START_TRAKING,
                    STOP_TRAKING));
        } else {
            manager.stopTracking();
            isStarted = false;
            listener.propertyChange(new PropertyChangeEvent(e.getSource(), Action.NAME, STOP_TRAKING,
                    START_TRAKING));
        }

    }
}
